package cn.echoedu.specifics;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.Image;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class SpecImage extends BaseSpec{
	private final String T="SpecImage-------->";
	@Override
	public <T> PreparedStatement getPreparedInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		L.gpi(T);
		Image currentimage=(Image)currentPojo;
		String sql="insert into "+tableName+"values(?,?,?)";
		L.p(T+sql);
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setInt(1,currentimage.getImageid());
		ps.setString(2,currentimage.getImagename());
		ps.setString(3,currentimage.getImagesrc());
		return ps;
	}

	@Override
	public <T> PreparedStatement getPreparedInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		final String M = "getPreparedInsertRunning********>>";
		L.gpir(T);
		Image currentimage=(Image)currentPojo;
		String sql="insert into "+tableName+" (imagename,imagesrc) values(?,?)";
		L.p(T+sql);
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setString(1,currentimage.getImagename());
		L.p(T+M+currentimage.getImagename());
		ps.setString(2,currentimage.getImagesrc());
		L.p(T+M+currentimage.getImagesrc());
		return ps;
	}

	@Override
	public Object getPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		L.gp(T);
		Image image=new Image();
		image.setImageid(rs.getInt(1));
		image.setImagename(rs.getString(2));
		image.setImagesrc(rs.getString(3));
		return image;
	}

	@Override
	public <T> PreparedStatement getPreparedUpdate(Connection con,
			TableName tableName, T oldPojo, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		L.gpu(T);
		Image newimage=(Image)currentPojo;
		Image oldimage=(Image)oldPojo;
		String sql="update image set imagename=?,imagesrc=? where imageid=?";
		L.p(T+sql);
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setString(1,newimage.getImagename());
		ps.setString(2,newimage.getImagesrc());
		ps.setInt(3,oldimage.getImageid());
		return ps;
	}

	@Override
	public Object getLeftJoinPojo(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getPojoBySql(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> String getPreparedInsertGetPrimaryKeySql(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub\
		final String M = "getPreparedInsertGetPrimaryKeySql***********>>";
		L.p(T+M);
		Image image = (Image)currentPojo;
		String sql = "insert into image(imagename,imagesrc) values"+
				StringUtil.getBeforeTheBsrackets()+StringUtil.varToSqlObject(image.getImagename())
				+StringUtil.getComma()+StringUtil.varToSqlObject(image.getImagesrc())
				+StringUtil.getAfterTheBrackets();
		L.p(T+M+sql);
		return sql;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsert(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> PreparedStatement getPreparedListInsertRunning(Connection con,
			TableName tableName, T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		List<Image> listimage = (List<Image>)currentPojo;
		String sql="insert into image(imagename,imagesrc) values(?,?)";
		PreparedStatement preparedstatement = con.prepareStatement(sql
				,PreparedStatement.RETURN_GENERATED_KEYS);
//            ,Statement.RETURN_GENERATED_KEYS);
		for(int i=0,len=listimage.size();i<len;i++){
			L.p("list image i:"+i+listimage.get(i).getImagename());
			if(listimage.get(i)!=null&&listimage.get(i).getImagename()!=null){
				preparedstatement.setString(1,listimage.get(i).getImagename());
			}else{
				preparedstatement.setNull(1,Types.VARCHAR);
			}
			if(listimage.get(i)!=null&&listimage.get(i).getImagesrc()!=null){
				preparedstatement.setString(2,listimage.get(i).getImagesrc());
			}else{
				preparedstatement.setNull(2, Types.VARCHAR);
			}
			preparedstatement.addBatch();
		}
		return preparedstatement;
	}

}
